﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using CommunitySystem.JinShengService;
using CommunitySystem.PublicOfficialSelectionService;
using EHR.Common;
using System.Collections.ObjectModel;
using System.IO;
using CommunitySystem.EmpbasicService;

namespace CommunitySystem.PageView
{
    public partial class AddjinshengWindow : ChildWindow
    {
       private JinShen_MainInfo entity = null;
       private JinShen_MainInfo Comentity = null;
       private ObservableCollection<JinShen_MainInfo> entitys = new ObservableCollection<JinShen_MainInfo>();
       private int _id = -1;
       private JinShengServiceClient _client = new JinShengServiceClient();
       private PublicOfficialSelectionServiceClient downLoadClient = null;
       private List<byte> fileContent = new List<byte>();
       private JinShen_MainInfo_Attatchment jinshenatt = null;

       private EmpbasicService.EmpBasicServiceClient Client2 = new EmpbasicService.EmpBasicServiceClient();
       public ObservableCollection<BianZhiModel> BianZhiModels = new ObservableCollection<BianZhiModel>();
    
       public AddjinshengWindow(int id, string button,List<string> orgIDs)
        {
            InitializeComponent();

           //编制数
            Client2.GetBianZhiDetailModelCompleted += new EventHandler<GetBianZhiDetailModelCompletedEventArgs>(Client2_GetBianZhiDetailModelCompleted);
           

            Page(null);

            //JinShenM M = new JinShenM() {  Status = CommunitySystem.PageView.JinShenConst.STATUS_Solution_Submit, CreateUser = Session.LoginUser.LoginName ,CreateDate=DateTime.Now.Date};
           
            //InitjinshengControl(M);  

            ////每个人登录进来所管辖的单位不同
           if (Comentity == null) this.dtorgid.DataContext = new JinShen_MainInfo();

            //dtbOrg.Parameter = new string[1] { "loginname=" + Session.LoginUser.LoginName };
           //dtbOrg.Field = "OrgName";

           #region
           if (orgIDs[0] == "0")
           {
               tbtiaojian.Visibility = System.Windows.Visibility.Collapsed;
               dtorgid.Visibility = System.Windows.Visibility.Visible;
           }
           else
           {
               tbtiaojian.Visibility = System.Windows.Visibility.Visible;
               dtorgid.Visibility = System.Windows.Visibility.Collapsed;
             
               string ord = "";
               foreach (string i in orgIDs)
               {
                   ord = ord + i + ",";
               }
               Client2.GetOrgForHRModelbyIDsCompleted += new EventHandler<EmpbasicService.GetOrgForHRModelbyIDsCompletedEventArgs>(Client2_GetOrgForHRModelbyIDsCompleted);
               Client2.GetOrgForHRModelbyIDsAsync("(" + ord.Substring(0, ord.Length - 1) + ")");
           }
           #endregion

           //保存审核相关记录
           _client.AddJinShen_SHJLDetailCompleted += (ss, ee) =>
           {
               SilverlightMessageBoxLibrary.Message.InfoMessage("保存成功");
               IsWorking.Visibility = Visibility.Collapsed;
               this.DialogResult = true;
           };

           //消息
           _client.AddSystemInfoCompleted += (ss, ee) => { 
                
           };
           ///保存
            _client.SaveJinShenMainInfosCompleted += (sender, e) =>
            {
                if (e.Error == null)
                {
                   
                    JinShen_SHJLDetail shjl = new JinShen_SHJLDetail();
                     shjl.Name = "方案审核";
                    _client.AddJinShen_SHJLDetailAsync(shjl,null);

                    //添加消息
                    SystemInfo sysinfo = new SystemInfo();
                    sysinfo.MenuId = 376;
                    sysinfo.MoKuai = "晋升管理";
                    sysinfo.MoKuaiSX = "方案审核";
                    sysinfo.DW1 = orgname;
                    sysinfo.CreateUser = Session.LoginUser.RealName;
                    sysinfo.CreateDate = DateTime.Now.ToString();
                    sysinfo.CreateUserID = 1;  //管理员角色
                    sysinfo.OrgIDs = orgid.ToString();
                    sysinfo.LogUID = Session.LoginUser.UserID;
                    _client.AddSystemInfoAsync(sysinfo);
        
                }
                else
                {
                    SilverlightMessageBoxLibrary.Message.ErrorMessage("保存失败，可能原因:\n" + e.Error.Message);
                    IsWorking.Visibility = Visibility.Collapsed;
                }
            };

            _client.SaveJinShenMainInfos1Completed += (sender, e) =>
            {
                //if (e.Error == null)
                //{
                //    SilverlightMessageBoxLibrary.Message.InfoMessage("保存成功");
                //    IsWorking.Visibility = Visibility.Collapsed;
                //    this.DialogResult = true;
                //}
                //else
                //{
                //    SilverlightMessageBoxLibrary.Message.ErrorMessage("保存失败，可能原因:\n" + e.Error.Message);
                //    IsWorking.Visibility = Visibility.Collapsed;
                //}
                //foreach (var item in atts)
                //{
                //    _client.UploadFile1Async(e.Result, item.Key, item.filename, item.TempUrl, item.realname);
                //}

                  foreach (var key in files.Keys)
                  {
                      var file = files[key];
                      if (file.Context.Count <= 0)
                      {
                          SilverlightMessageBoxLibrary.Message.ErrorMessage("上传的文件内容不能为空!");
                          IsWorking.Visibility = Visibility.Collapsed;
                          return;
                      }
                    
                      _client.UploadFile1Async(e.Result, "", "", file.Context[0], "");
                      file.Context.RemoveAt(0);

                  }
            };
            _client.UploadFile1Completed += (sender, e) => {

                if (e.Error == null)
                {
                    SilverlightMessageBoxLibrary.Message.InfoMessage("保存成功");
                    IsWorking.Visibility = Visibility.Collapsed;
                    this.DialogResult = true;
                }
                else
                {
                    SilverlightMessageBoxLibrary.Message.ErrorMessage("保存失败，可能原因:\n" + e.Error.Message);
                    IsWorking.Visibility = Visibility.Collapsed;
                }
            };

            this.Loaded += new RoutedEventHandler(AddjinshengWindow_Loaded);
        }
       /// <summary>
       /// 编制数
       /// </summary>
       /// <param name="sender"></param>
       /// <param name="e"></param>
       void Client2_GetBianZhiDetailModelCompleted(object sender, GetBianZhiDetailModelCompletedEventArgs e)
       {
           BianZhiModels = e.Result;
           if (BianZhiModels != null && BianZhiModels.Count > 0)
           {
               if (BianZhiModels.Where(p => p.ZhengKeLingDaoTC == true).Count() > 0)
               {
                   foreach (BianZhiModel i in BianZhiModels.Where(p => p.ZhengKeLingDaoTC == true))
                   {
                       int zhiwhu = !string.IsNullOrEmpty(i.ZhengKeLingDao) ? Convert.ToInt32(i.ZhengKeLingDao) : 0;
                       int renshu = BianZhiModels.Where(p => p.ZhengKeLingDaoTC == true).Sum(
                           p => !string.IsNullOrEmpty(p.zhengkeshiyou) ? Convert.ToInt32(p.zhengkeshiyou) : 0);
                       int jieyong = BianZhiModels.Where(p => p.ZhengKeLingDaoTC == true).Sum(
                           p =>
                           p.ZhengKeJieYongQingKuang != "无" ? Convert.ToInt32(p.ZhengKeJieYongQingKuang) : 0);
                       int chaozhi = BianZhiModels.Where(p => p.ZhengKeLingDaoTC == true).Sum(
                           p =>
                           p.ZhengKeChaoZhiQingKuang != "无" ? Convert.ToInt32(p.ZhengKeChaoZhiQingKuang) : 0);

                       i.ZhengKeShiJi =
                           (zhiwhu - renshu + jieyong + chaozhi)
                               .ToString();
                   }
               }

               if (BianZhiModels.Where(p => p.ZhengKeFeiLingDaoTC == true).Count() > 0)
               {
                   foreach (BianZhiModel i in BianZhiModels.Where(p => p.ZhengKeFeiLingDaoTC == true))
                   {
                       i.FuKeShiJi =
                           (Convert.ToInt32(
                               BianZhiModels.Where(p => p.ZhengKeFeiLingDaoTC == true).OrderBy(p => p.FuKeShiJi).
                                   FirstOrDefault().FuKeShiJi) +
                            BianZhiModels.Sum(
                                p => p.FuKeJieYongQingKuang != "无" ? Convert.ToInt32(p.FuKeJieYongQingKuang) : 0) +
                            BianZhiModels.Sum(
                                p => p.FuKeChaoZhiQingKuang != "无" ? Convert.ToInt32(p.FuKeChaoZhiQingKuang) : 0))
                               .ToString();

                       int zhiwhu = !string.IsNullOrEmpty(i.ZhengKeFeiLingDao) ? Convert.ToInt32(i.ZhengKeFeiLingDao) : 0;
                       int renshu = BianZhiModels.Where(p => p.ZhengKeFeiLingDaoTC == true).Sum(
                           p => !string.IsNullOrEmpty(p.fukeshiyou) ? Convert.ToInt32(p.fukeshiyou) : 0);
                       int jieyong = BianZhiModels.Where(p => p.ZhengKeFeiLingDaoTC == true).Sum(
                           p =>
                           p.FuKeJieYongQingKuang != "无" ? Convert.ToInt32(p.FuKeJieYongQingKuang) : 0);
                       int chaozhi = BianZhiModels.Where(p => p.ZhengKeFeiLingDaoTC == true).Sum(
                           p =>
                           p.FuKeChaoZhiQingKuang != "无" ? Convert.ToInt32(p.FuKeChaoZhiQingKuang) : 0);

                       i.ZhengKeShiJi =
                           (zhiwhu - renshu + jieyong + chaozhi)
                               .ToString();
                   }
               }
           }
       }

       void Client2_GetOrgForHRModelbyIDsCompleted(object sender, EmpbasicService.GetOrgForHRModelbyIDsCompletedEventArgs e)
       {
           tbtiaojian.ItemsSource = e.Result;
           //for (int i = 0; i < tbtiaojian.Items.Count; i++)
           //{
           //    if (((OrgForHRModel)(tbtiaojian.Items[i])).ID
           //        == model.OrgID)
           //    {
           //        tbtiaojian.SelectedIndex = i;
           //    }
           //}
       }
       private PublicOfficialSelectionService.PublicOfficialSelectionServiceClient _uploadClient = null;
        void AddjinshengWindow_Loaded(object sender, RoutedEventArgs e)
        {
            _uploadClient = new PublicOfficialSelectionService.PublicOfficialSelectionServiceClient();
            if (_id > 0)
            {
                this.Title = "编辑晋升信息";
               
            }
            else
            {
                this.Title = "新增晋升信息";
               
            }
        }
       private AddJinshengControl c = null;
        private void InitjinshengControl(JinShenM M=null)
        {
            entity = new JinShen_MainInfo() { Status = CommunitySystem.PageView.JinShenConst.STATUS_Solution_Submit, CreateUser = Session.LoginUser.LoginName,OrgID=orgid };
            if (M != null)
                entity.JinShenM = M;
            entitys.Add(entity);
            c = new AddJinshengControl(entity, entitys,orgid);
            spJs.Children.Add(c);
        }
        public bool Vilidata()
        {
            if (string.IsNullOrEmpty(orgname))
            {
                SilverlightMessageBoxLibrary.Message.ErrorMessage("单位不能为空!");
                return false;
            }
            #region 编制数判断
            foreach (var model in entitys)
            {
                if (model.PositionLevel == "乡科级正职（主任科员）" && model.PositionAttibute == "领导职务")
                {
                    if (BianZhiModels.Where(p => p.OrgID == orgid && p.JiGouMingCheng == model.Department && Convert.ToInt32(!string.IsNullOrEmpty(p.ZhengKeShiJi) ? p.ZhengKeShiJi : "0") > 0).FirstOrDefault() == null)
                    {
                        SilverlightMessageBoxLibrary.Message.ErrorMessage(model.Department+"科室没有对应的职数可用！");
                        return false;
                    }
                }
                if (model.PositionLevel == "乡科级副职（副主任科员）" && model.PositionAttibute == "领导职务")
                {
                    if (BianZhiModels.Where(p => p.OrgID == orgid && p.JiGouMingCheng == model.Department && Convert.ToInt32(!string.IsNullOrEmpty(p.FuKeShiJi) ? p.FuKeShiJi : "0") > 0).FirstOrDefault() == null)
                    {
                        SilverlightMessageBoxLibrary.Message.ErrorMessage(model.Department + "科室没有对应的职数可用！");
                        return false;
                    }
                }
                if (model.PositionLevel == "乡科级正职（主任科员）" && model.PositionAttibute == "非领导职务")
                {
                    if (BianZhiModels.Where(p => p.OrgID == orgid && p.JiGouMingCheng == model.Department && Convert.ToInt32(!string.IsNullOrEmpty(p.ZhuRenShiJi) ? p.ZhuRenShiJi : "0") > 0).FirstOrDefault() == null)
                    {
                        SilverlightMessageBoxLibrary.Message.ErrorMessage(model.Department + "科室没有对应的职数可用！");
                        return false;
                    }
                }
                if (model.PositionLevel == "乡科级副职（副主任科员）" && model.PositionAttibute == "非领导职务")
                {
                    if (BianZhiModels.Where(p => p.OrgID == orgid && p.JiGouMingCheng == model.Department && Convert.ToInt32(!string.IsNullOrEmpty(p.FuZhuRenShiJi) ? p.FuZhuRenShiJi : "0") > 0).FirstOrDefault() == null)
                    {
                        SilverlightMessageBoxLibrary.Message.ErrorMessage(model.Department + "科室没有对应的职数可用！");
                        return false;
                    }
                }
            }
            #endregion
            foreach (var entity in entitys)
            {
                if (string.IsNullOrEmpty(entity.Department) && !entity.PositionAttibute.Equals("非领导职务"))
            {
                SilverlightMessageBoxLibrary.Message.ErrorMessage(entity.Department+"科室"+"不能为空!");
                return false;
            }
            if (string.IsNullOrEmpty(entity.Position))
            {
                SilverlightMessageBoxLibrary.Message.ErrorMessage(entity.Department+"科室"+"职位不能为空!");
                return false;
            }
            if (string.IsNullOrEmpty(entity.PositionAttibute))
            {
                SilverlightMessageBoxLibrary.Message.ErrorMessage(entity.Department+"科室"+"职务属性不能为空!");
                return false;
            }
            if (string.IsNullOrEmpty(entity.PositionLevel))
            {
                SilverlightMessageBoxLibrary.Message.ErrorMessage(entity.Department+"科室"+"职务层次不能为空!");
                return false;
            }
            if (string.IsNullOrEmpty(entity.Education))
            {
                SilverlightMessageBoxLibrary.Message.ErrorMessage(entity.Department+"科室"+"文化程度不能为空!");
                return false;
            }
            if (string.IsNullOrEmpty(entity.Political))
            {
                SilverlightMessageBoxLibrary.Message.ErrorMessage(entity.Department+"科室"+"政治面貌不能为空!");
                return false;
            }
            if (string.IsNullOrEmpty(entity.PromotType))
            {
                SilverlightMessageBoxLibrary.Message.ErrorMessage(entity.Department+"科室"+"晋升方式不能为空");
                return false;
            }

            }
    
            return true;
        }private ObservableCollection<JinShen_MainInfo> entitysC = null;
        private void OKButton_Click(object sender, RoutedEventArgs e)
        {
            if (Vilidata())
            {
                if (entitys.First().PromotType != null)
                {
                    if (entitys.Where(p => p.PromotType == "竞争上岗").ToList().Count > 0)
                    {
                        var isjzsgModel = entitys.Where(p => p.PromotType == "竞争上岗").ToList().First();
                        if (isjzsgModel != null)
                        {
                            if (isjzsgModel.PromotType == "竞争上岗" && _files.Count()==0)
                            {
                                SilverlightMessageBoxLibrary.Message.ErrorMessage("此晋升方案有竞争上岗的科室,必须上传方案附件!");
                                return ;
                            }
                        }
                    }
                }
                if (_files != null&&_files.Count!=0)
                {
                    if (_files.Where(p => p.State == Constants.FileStates.Pending).Count() > 0)
                    {
                        SilverlightMessageBoxLibrary.Message.ErrorMessage("您所选的附件还未上传，请点击上传按钮进行上传！");
                        return;
                    }
                    if (_files.Where(p => p.State == Constants.FileStates.Uploading).Count() > 0)
                    {
                        SilverlightMessageBoxLibrary.Message.ErrorMessage("附件正在上传，请稍后！");
                        return;
                    }
                    else
                    {
                        if (entity.Attachment == null)
                            entity.Attachment = new System.Collections.ObjectModel.ObservableCollection<JinShen_MainInfo_Attatchment>();
                        else
                            entity.Attachment.Clear();
                        foreach (var item in _files.Where(p => p.State == Constants.FileStates.Finished).ToList())
                        {

                            var att = new JinShen_MainInfo_Attatchment() { realname = item.FileNameG, filename = item.FileName, Key = fileDictionary, Status = (item.State).ToString(), FileSize = item.FileSize.ToString(), Percentage = item.Percentage.ToString() };
                            entity.Attachment.Add(att);
                        }
                        if (entity.Attachment.Count != 0)
                        jinshenatt = entity.Attachment.First();
                    }
                }

                entitysC = new ObservableCollection<JinShen_MainInfo>();
                foreach (var item in entitys)
                {
                    item.OrgID = orgid;
                    item.OrgName = orgname;
                    entitysC.Add(item);
                }
                _client.SaveJinShenMainInfosAsync(entitysC, 0, jinshenatt);

            }
        }
        void _uploadClient_UploadFileCompleted(object sender, PublicOfficialSelectionService.UploadFileCompletedEventArgs e)
        {
            var file = e.UserState as UploadFileInfo;
            if (file.Context.Count > 0)
            {
                _uploadClient.UploadFileAsync(fileDictionary, file.DisplayName, file.Context[0], false, file);
                file.Context.RemoveAt(0);
            }
          
        }
      
        private void CancelButton_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = false;
        }
    

      /// <summary>
      /// 一次添加多个岗位
      /// </summary>
      /// <param name="sender"></param>
      /// <param name="e"></param>
        private void img_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            JinShenM M = new JinShenM() { Status = CommunitySystem.PageView.JinShenConst.STATUS_Solution_Submit, CreateUser = Session.LoginUser.LoginName, CreateDate = DateTime.Now.Date };
            InitjinshengControl(M);
        }

        private readonly string fileDictionary = "file";
        private Dictionary<string, UploadFileInfo> files = new Dictionary<string, UploadFileInfo>();
        private ObservableCollection<JinShen_MainInfo_Attatchment> atts = new ObservableCollection<JinShen_MainInfo_Attatchment>();
       
        public int orgid = -1;
        public string orgname = "";
        private void dtbOrg_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {

            if (e.PropertyName.Equals("SelectItem") && dtorgid.SelectItem != null)
            {
                entity.OrgID = dtorgid.SelectItem.Id;
                orgname = dtorgid.SelectItem.DictCode;
                orgid = dtorgid.SelectItem.Id;

                //编制数
                Client2.GetBianZhiDetailModelAsync(orgid.ToString(), "");
            }
          
        }

        private void tbtiaojian_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (this.tbtiaojian.SelectedItem != null)
            {
                if(entity!=null)
                entity.OrgID = (tbtiaojian.SelectedItem as OrgForHRModel).ID;

                orgname = (tbtiaojian.SelectedItem as OrgForHRModel).Name;
                orgid =Convert.ToInt32((tbtiaojian.SelectedItem as OrgForHRModel).ID);

                entitys.Clear();
                spJs.Children.Clear();

                JinShenM M = new JinShenM() { Status = CommunitySystem.PageView.JinShenConst.STATUS_Solution_Submit, CreateUser = Session.LoginUser.LoginName, CreateDate = DateTime.Now.Date };
                InitjinshengControl(M);

                //编制数
                Client2.GetBianZhiDetailModelAsync(orgid.ToString(), "");
            }
        }
        #region  上传附件
        private int _maxFileSize = int.MaxValue;

        private FileCollection _files;
        private int _maxUpload = 4;
        private string _customParams;
        private string _fileFilter;

        public void Page(IDictionary<string, string> initParams)
        {
            _files = new FileCollection(_customParams, _maxUpload);
            if (entity != null)
            {
                if (entity.Attachment != null)
                {
                    if (entity.Attachment.Count > 0)
                    {
                        foreach (var item in entity.Attachment)
                        {
                            var uf = new UserFile() { FileName = item.filename, FileNameG = item.realname, State = Constants.FileStates.Finished, Percentage = Convert.ToInt32(item.Percentage), BytesUploaded = Convert.ToDouble(item.FileSize), FileSize = Convert.ToDouble(item.FileSize) };

                            _files.Add(uf);
                        }
                    }
                }
            }

            FileList.ItemsSource = _files;

        }
        /// <summary>
        /// 方案上报
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void hlb_Click(object sender, RoutedEventArgs e)
        {
            #region
            //files.Clear();
            //atts.Clear();
            //wp.Children.Clear();
            //if (entity.Attachment != null)
            //{
            //    if (entity.Attachment.Count > 0)
            //        entity.Attachment.Clear();
            //}

            //var openFile = new OpenFileDialog() { Multiselect = false };
            //if (openFile.ShowDialog() == true)
            //{

            //    string GuidName = Guid.NewGuid().ToString("N");
            //    UploadFileInfo uploadFile = new UploadFileInfo() { DisplayName = GuidName, Name = GuidName };
            //    var stream = openFile.File.OpenRead();
            //    uploadFile.Size = stream.Length / 16000;
            //    uploadFile.Context = new List<byte[]>();
            //    int b = 0;

            //    while (stream.Position > -1 && stream.Position < stream.Length)
            //    {
            //        if (stream.Length - stream.Position >= 16000)
            //        {
            //            b = 16000;
            //        }
            //        else
            //        {
            //            b = (int)(stream.Length - stream.Position);
            //        }

            //        byte[] filebyte = new byte[b];
            //        stream.Read(filebyte, 0, b);
            //        uploadFile.Context.Add(filebyte);
            //    }
            //    stream.Close();
            //    files.Add(GuidName, uploadFile);




            //    StackPanel sp = new StackPanel() { Orientation = Orientation.Horizontal, };
            //    sp.Children.Add(new TextBlock() { Text = openFile.File.Name, });
            //    Image img = new Image() { Cursor = Cursors.Hand, Height = 15, Width = 15, Margin = new Thickness(5, 0, 0, 0) };
            //    img.Source = new System.Windows.Media.Imaging.BitmapImage(
            //        new Uri("/CommunitySystem;component/Images/ViewDesign/delete.png",
            //            UriKind.RelativeOrAbsolute));
            //    sp.Children.Add(img);
            //    if (entity.Attachment == null)
            //        entity.Attachment = new System.Collections.ObjectModel.ObservableCollection<JinShengService.JinShen_MainInfo_Attatchment>();

            //    var att = new JinShengService.JinShen_MainInfo_Attatchment() { realname = openFile.File.Name, filename = "", Key = fileDictionary };
            //    entity.Attachment.Add(att);

            //    atts.Add(att);
            //    jinshenatt = entity.Attachment.First();
            //    img.MouseLeftButtonDown += (sender1, e1) =>
            //    {
            //        wp.Children.Remove(sp);
            //        entity.Attachment.Remove(att);
            //        atts.Remove(att);
            //        jinshenatt = null;
            //    };
            //    wp.Children.Add(sp);

            //}
            #endregion

            #region
            if(_files!=null)
            _files.Clear();
         
            OpenFileDialog ofd = new OpenFileDialog() { Multiselect=false};
     
            try
            {
                if (!string.IsNullOrEmpty(_fileFilter))
                    ofd.Filter = _fileFilter;
            }
            catch (ArgumentException ex)
            {
                //User supplied a wrong configuration file
                throw new Exception("Wrong file filter configuration.", ex);
            }

            if (ofd.ShowDialog() == true)
            {

                foreach (FileInfo file in ofd.Files)
                {
                    string GuidName = Guid.NewGuid().ToString("N");
                    string fileName = file.Name;

                    UserFile userFile = new UserFile();
                    userFile.FileName = GuidName; //GuidName file.Name;
                    userFile.FileNameG = fileName;
                    userFile.IsAdd = true;
                    userFile.FileStream = file.OpenRead();


                    #region  页面显示
                    foreach (var item in _files)
                    {
                        if (item.FileNameG.Contains(fileName))
                        {
                            SilverlightMessageBoxLibrary.CustomMessage cm = new SilverlightMessageBoxLibrary.CustomMessage("此文件已上传,需要覆盖原有文件吗?", SilverlightMessageBoxLibrary.CustomMessage.MessageType.Confirm, null);
                            cm.Show();
                            cm.OKButton.Click += (sender1, e1) =>
                            {
                                _files.Remove(item);
                            };
                        }
                    }
                    #endregion

                    if (userFile.FileStream.Length <= _maxFileSize)
                    {
                        //向文件列表中添加文件信息
                        if (_files == null) _files = new FileCollection(_customParams, _maxUpload);
                        _files.Add(userFile);
                    }
                    else
                        SilverlightMessageBoxLibrary.Message.InfoMessage("上传最大文件大小：" + (_maxFileSize / 1024).ToString() + "KB.");
                }
            }
            #endregion
        }

        /// <summary>
        /// 上传附件开始
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ibtnFJ_Click(object sender, RoutedEventArgs e)
        {
            if (_files.Count == 0)
            {
                SilverlightMessageBoxLibrary.Message.ErrorMessage("请选择上传的附件！");
                return;
            }
            else
            {
      
                _files.UploadFiles();
            }
        }
        #endregion
    }
}

